#pragma once
#include <assert.h>
#include <iostream>

namespace ly
{
	class string
	{
	public:
		friend ostream& operator<<(ostream& _out, const ly::string& s);


		string(const char* str = "")
			: _size(strlen(str))
			, _capacity(_size)
		{
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}

		string(const string& s)
		{
			_str = new char[_capacity + 1];
			strcpy(_str, s._str);

			_capacity = s._capacity;
			_size = s._size;
		}

		string& operator=(const string& s)
		{
			if (_str != s._str)
			{
				char* tmp = new char[s._capacity + 1];
				delete[] _str;

				strcpy(tmp, s._str);
				_str = tmp;
				_capacity = s._capacity;
				_size = s._size;
			}

			return *this;
		}

		~string()
		{
			delete[] _str;
			_str = nullptr;
			_capacity = _size = 0;
		}

		const char* c_str() const 
		{
			return _str;
		}

		const size_t size() const
		{
			return _size;
		}

		const size_t capacity() const
		{
			return _capacity;
		}

		typedef char* iterator;
		typedef const char* const_iterator;

		iterator begin()
		{
			return _str;
		}

		const_iterator begin() const
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator end() const
		{
			return _str + _size;
		}

		char& operator[](size_t index) const
		{
			assert(index < _size);

			return *(_str + index);
		}


	private:
		char* _str;
		size_t _size;
		size_t _capacity;
	};
}


